package com.abel.serviceuser.controller;

import com.abel.servicemodel.user.Login;
import com.abel.serviceuser.service.LoginService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Max;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.locks.Lock;

@RestController
@Api(value = "用户登录",description = "用户登录",tags = "userLogin")
public class User {
    @Autowired
   private LoginService loginService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${server.port}")
    private String port;
    @GetMapping("/login")
    @ApiOperation(value = "用户登录",notes = "用户登录")
    public Login login(@RequestParam(name = "username") String username, @RequestParam(name = "pwd") String pwd,HttpServletRequest request){
       Login login= loginService.login(username,pwd);
       HttpSession session= request.getSession();
    if(login!=null) {
        session.setAttribute("user", login);
        System.out.println(session.getId());
        System.out.println("SESSION_CODE:"+session.getId() + " " + session);
    }
        System.out.println("port:"+port);
        return login;
       }
 /*      @GetMapping("/save")
       @ApiOperation(value = "测试redis",notes = "测试redis")
       public  String testRedis(HttpServletRequest request){
        HttpSession session=request.getSession();
        Object object=session.getAttribute("user");
        if (object==null){
            return "请登录";
        }
         String key="key1";
        Login login=new Login();
         login.setId(1).setCrt_time(new Date()).setUsername("qwe").setPwd("123456");
         Object value= redisTemplate.opsForValue().get(key);
           if (value==null){
               redisTemplate.opsForValue().set(key,login);
           }
           return value.toString();
       }*/
}
